package com.multimedia.scala.euler
/*
145 is a curious number, as 1! + 4! + 5! = 1 + 24 + 120 = 145.

Find the sum of all numbers which are equal to the sum of the factorial of their digits.

Note: as 1! = 1 and 2! = 2 are not sums they are not included.
 */
import com.multimedia.scala.TailRecTest
object Asd34 {
  def main(args : Array[String]) : Unit = {
    def sumFactorial(x: BigInt) =
      x.toString().toCharArray.map(_.asDigit).map(TailRecTest.factorialBig(_)).sum

    (10 to 9999999).filter(x=>x==sumFactorial(x)).foreach(println)
    //println(sumFactorial(9))
    //max is 7 digits
    //println(findBounds)
   
  }
  /** display max digit that can be produced by factorials */
  def findBounds {
    def maxDigit(n: BigInt, acc: BigInt = 9): BigInt = {
      if (n==1) acc
      else maxDigit(n-1, acc*10+9)
    }
    def maxFactorial(n: BigInt) = n*TailRecTest.factorialBig(9)
    
    //(1 to 1000).map(BigInt(_)).takeWhile(x=>maxFactorial(x)>maxDigit(x)).foreach(println)
    println(maxFactorial(7))
    println(maxDigit(7))
  }
}
